import 'package:flutter/material.dart';

import 'package:dio/dio.dart';
import 'dart:convert';

class CinemaList extends StatefulWidget {
  @override
  _CinemaListState createState() => _CinemaListState();
}

class _CinemaListState extends State<CinemaList> {
  // 定义当前页面数据
  List cinemas = [];

  // 存储当前页面需要的三种颜色
  Color color0 = Color.fromARGB(255, 240, 61, 55);
  Color color1 = Color.fromARGB(255, 255, 153, 0);
  Color color2 = Color.fromARGB(255, 88, 157, 175);

  // tag 标签数组
  List<Widget> tagWidgets(tag) {
    List<Widget> tagList = [];
    if (tag["allowRefund"] == 1) {
      tagList.add(CinemaTag(
        color: color2,
        text: '退',
      ));
    }
    if (tag["endorse"] == 1) {
      tagList.add(CinemaTag(
        color: color2,
        text: '改签',
      ));
    }
    if (tag["snack"] == 1) {
      tagList.add(CinemaTag(
        color: color1,
        text: '小吃',
      ));
    }
    if (tag["vipTag"] is String && tag["vipTag"].length > 0) {
      tagList.add(CinemaTag(
        color: color1,
        text: tag["vipTag"],
      ));
    }
    if(tag["hallType"] is List) {
      tag["hallType"].forEach((str) {
        tagList.add(CinemaTag(
          color: color2,
          text: str,
        ));
      });
    }
    
    return tagList;
  }

  // 获取数据
  void getCinemaList() async {
    try {
      Dio dio = new Dio();
      Response response = await dio.get('http://m.maoyan.com/ajax/cinemaList?day=2019-09-30&offset=0&limit=20&districtId=-1&lineId=-1&hallType=-1&brandId=-1&serviceId=-1&areaId=-1&stationId=-1&item=&updateShowDay=true&reqId=1569859159970&cityId=44');
      Map responseData = jsonDecode(response.toString());
      setState(() {
        cinemas = responseData['cinemas'];
      });
    } catch (e) {
      print(e);
    }
  }
  @override
  void initState() {
    getCinemaList();
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return Container(
      child: ListView.builder(
        itemCount: cinemas.length,
        itemBuilder: (BuildContext context, int i) {
          Map _cinema = cinemas[i];
          return Container(
            padding: EdgeInsets.all(10),
            child: Column(
              children: <Widget>[
                Padding(
                  padding: EdgeInsets.only(bottom: 3),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.end,
                    children: <Widget>[
                      Text(
                        '${_cinema["nm"]}',
                        style: TextStyle(fontSize: 16),
                      ),
                      Text(
                        '${_cinema["sellPrice"]}',
                        style: TextStyle(color: color0, fontSize: 18),
                      ),
                      Text(
                        '元起',
                        style: TextStyle(color: color0, fontSize: 12),
                      ),
                    ],
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(bottom: 3),
                  child: Row(
                    children: <Widget>[
                      Expanded(
                        child: Text(
                          '${_cinema["addr"]}',
                          overflow: TextOverflow.ellipsis,
                          maxLines: 1,
                          style: TextStyle(
                            color: Color.fromARGB(255, 102, 102, 102)
                          ),
                        ),
                      ),
                      Text('${_cinema["distance"]}'),
                    ],
                  ),
                ),
                Padding(
                  padding: EdgeInsets.only(bottom: 3),
                  child: Row(
                    children: tagWidgets(_cinema["tag"]),
                  ),
                ),
                _cinema["promotion"]["cardPromotionTag"] is String ? 
                Row(
                  children: <Widget>[
                    Container(
                      width: 17,
                      height: 17,
                      margin: EdgeInsets.only(right: 5),
                      child: Image(
                        fit: BoxFit.cover,
                        image: AssetImage('images/card.png'),
                      ),
                    ),
                    Text(
                      _cinema["promotion"]["cardPromotionTag"],
                      style: TextStyle(
                          fontSize: 13,
                          color: Color.fromARGB(255, 153, 153, 153)),
                    )
                  ],
                ) : Container(),
              ],
            ),
          );
        },
      ),
    );
  }
}

class CinemaTag extends StatelessWidget {
  CinemaTag({Key key, @required this.text, @required this.color})
      : super(key: key);
  final String text;
  final Color color;
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(right: 3),
      decoration: BoxDecoration(
        border: Border.all(
          color: color,
        ),
        borderRadius: BorderRadius.all(
          Radius.circular(2),
        ),
      ),
      padding: EdgeInsets.fromLTRB(2, 0, 2, 1),
      child: Text(
        text,
        style: TextStyle(color: color, fontSize: 10),
      ),
    );
  }
}
